<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册 - 超级AI面试官</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            padding: 20px;
        }

        .register-container {
            background: white;
            padding: 40px;
            border-radius: 20px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
            animation: slideUp 0.6s ease-out;
        }

        @keyframes slideUp {
            from {
                opacity: 0;
                transform: translateY(30px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .logo {
            text-align: center;
            margin-bottom: 30px;
        }

        .logo h1 {
            color: #333;
            font-size: 28px;
            margin-bottom: 10px;
        }

        .logo p {
            color: #666;
            font-size: 16px;
        }

        .form-row {
            display: flex;
            gap: 15px;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 20px;
            flex: 1;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            color: #333;
            font-weight: 500;
        }

        .form-group input {
            width: 100%;
            padding: 15px;
            border: 2px solid #e1e5e9;
            border-radius: 10px;
            font-size: 16px;
            transition: all 0.3s ease;
            background: #f8f9fa;
        }

        .form-group input:focus {
            outline: none;
            border-color: #667eea;
            background: white;
            box-shadow: 0 0 0 3px rgba(102, 126, 234, 0.1);
        }

        .password-strength {
            margin-top: 5px;
            font-size: 12px;
            color: #666;
        }

        .password-strength.weak {
            color: #ff4757;
        }

        .password-strength.medium {
            color: #ffa502;
        }

        .password-strength.strong {
            color: #2ed573;
        }

        .register-btn {
            width: 100%;
            padding: 15px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            border: none;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 600;
            cursor: pointer;
            transition: transform 0.2s ease;
            margin-bottom: 20px;
        }

        .register-btn:hover {
            transform: translateY(-2px);
            box-shadow: 0 8px 25px rgba(102, 126, 234, 0.3);
        }

        .register-btn:disabled {
            opacity: 0.6;
            cursor: not-allowed;
            transform: none;
        }

        .divider {
            text-align: center;
            margin: 25px 0;
            position: relative;
            color: #999;
        }

        .divider::before {
            content: '';
            position: absolute;
            top: 50%;
            left: 0;
            right: 0;
            height: 1px;
            background: #e1e5e9;
        }

        .divider span {
            background: white;
            padding: 0 15px;
        }

        .login-link {
            text-align: center;
        }

        .login-link a {
            color: #667eea;
            text-decoration: none;
            font-weight: 500;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        .error-message {
            background: #fee;
            color: #c33;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 15px;
            border: 1px solid #fcc;
            display: none;
        }

        .success-message {
            background: #efe;
            color: #3c3;
            padding: 10px;
            border-radius: 8px;
            margin-bottom: 15px;
            border: 1px solid #cfc;
            display: none;
        }

        .field-error {
            border-color: #ff4757 !important;
        }

        .field-error-message {
            color: #ff4757;
            font-size: 12px;
            margin-top: 5px;
        }
    </style>
</head>
<body>
    <div class="register-container">
        <div class="logo">
            <h1>🤖 超级AI面试官</h1>
            <p>智能招聘，精准匹配</p>
        </div>

        <div id="error-message" class="error-message"></div>
        <div id="success-message" class="success-message"></div>

        <form id="register-form">
            <div class="form-group">
                <label for="username">用户名</label>
                <input type="text" id="username" name="username" required placeholder="请输入用户名（3-20个字符）">
                <div class="field-error-message" id="username-error"></div>
            </div>

            <div class="form-row">
                <div class="form-group">
                    <label for="realName">真实姓名</label>
                    <input type="text" id="realName" name="realName" required placeholder="请输入真实姓名">
                    <div class="field-error-message" id="realName-error"></div>
                </div>

                <div class="form-group">
                    <label for="phone">手机号码</label>
                    <input type="tel" id="phone" name="phone" required placeholder="请输入手机号码">
                    <div class="field-error-message" id="phone-error"></div>
                </div>
            </div>

            <div class="form-group">
                <label for="email">邮箱地址</label>
                <input type="email" id="email" name="email" required placeholder="请输入邮箱地址">
                <div class="field-error-message" id="email-error"></div>
            </div>

            <div class="form-group">
                <label for="password">密码</label>
                <input type="password" id="password" name="password" required placeholder="请输入密码（至少6位）">
                <div class="password-strength" id="password-strength"></div>
                <div class="field-error-message" id="password-error"></div>
            </div>

            <div class="form-group">
                <label for="confirmPassword">确认密码</label>
                <input type="password" id="confirmPassword" name="confirmPassword" required placeholder="请再次输入密码">
                <div class="field-error-message" id="confirmPassword-error"></div>
            </div>

            <button type="submit" class="register-btn" id="register-btn">立即注册</button>
        </form>

        <div class="divider">
            <span>已有账号？</span>
        </div>

        <div class="login-link">
            <a href="/login">立即登录</a>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const registerForm = document.getElementById('register-form');
            const registerBtn = document.getElementById('register-btn');
            const errorMessage = document.getElementById('error-message');
            const successMessage = document.getElementById('success-message');
            const passwordInput = document.getElementById('password');
            const passwordStrength = document.getElementById('password-strength');

            function showError(message) {
                errorMessage.textContent = message;
                errorMessage.style.display = 'block';
                successMessage.style.display = 'none';
            }

            function showSuccess(message) {
                successMessage.textContent = message;
                successMessage.style.display = 'block';
                errorMessage.style.display = 'none';
            }

            function hideMessages() {
                errorMessage.style.display = 'none';
                successMessage.style.display = 'none';
            }

            function showFieldError(fieldId, message) {
                const field = document.getElementById(fieldId);
                const errorDiv = document.getElementById(fieldId + '-error');
                field.classList.add('field-error');
                errorDiv.textContent = message;
            }

            function hideFieldError(fieldId) {
                const field = document.getElementById(fieldId);
                const errorDiv = document.getElementById(fieldId + '-error');
                field.classList.remove('field-error');
                errorDiv.textContent = '';
            }

            function clearAllFieldErrors() {
                const fields = ['username', 'realName', 'phone', 'email', 'password', 'confirmPassword'];
                fields.forEach(fieldId => hideFieldError(fieldId));
            }

            function checkPasswordStrength(password) {
                if (password.length === 0) {
                    passwordStrength.textContent = '';
                    passwordStrength.className = 'password-strength';
                    return;
                }

                let strength = 0;
                if (password.length >= 8) strength++;
                if (/[a-z]/.test(password)) strength++;
                if (/[A-Z]/.test(password)) strength++;
                if (/[0-9]/.test(password)) strength++;
                if (/[^a-zA-Z0-9]/.test(password)) strength++;

                if (strength < 2) {
                    passwordStrength.textContent = '密码强度：弱';
                    passwordStrength.className = 'password-strength weak';
                } else if (strength < 4) {
                    passwordStrength.textContent = '密码强度：中等';
                    passwordStrength.className = 'password-strength medium';
                } else {
                    passwordStrength.textContent = '密码强度：强';
                    passwordStrength.className = 'password-strength strong';
                }
            }

            passwordInput.addEventListener('input', function() {
                checkPasswordStrength(this.value);
            });

            async function checkUsernameAvailability(username) {
                try {
                    const response = await fetch(`/api/users/check-username?username=${encodeURIComponent(username)}`);
                    const data = await response.json();
                    return data.available;
                } catch (error) {
                    console.error('检查用户名失败:', error);
                    return true;
                }
            }

            async function checkEmailAvailability(email) {
                try {
                    const response = await fetch(`/api/users/check-email?email=${encodeURIComponent(email)}`);
                    const data = await response.json();
                    return data.available;
                } catch (error) {
                    console.error('检查邮箱失败:', error);
                    return true;
                }
            }

            function validateForm() {
                clearAllFieldErrors();
                let isValid = true;

                const username = document.getElementById('username').value.trim();
                // const realName = document.getElementById('realName').value.trim(); // 暂时注释，后端没有此字段
                const phone = document.getElementById('phone').value.trim();
                const email = document.getElementById('email').value.trim();
                const password = document.getElementById('password').value;
                const confirmPassword = document.getElementById('confirmPassword').value;

                // 用户名验证
                if (username.length < 3 || username.length > 20) {
                    showFieldError('username', '用户名长度必须在3-20个字符之间');
                    isValid = false;
                }

                // 真实姓名验证（暂时移除，因为后端DTO中没有此字段）
                // if (realName.length < 2 || realName.length > 10) {
                //     showFieldError('realName', '真实姓名长度必须在2-10个字符之间');
                //     isValid = false;
                // }

                // 手机号验证
                const phoneRegex = /^1[3-9]\d{9}$/;
                if (!phoneRegex.test(phone)) {
                    showFieldError('phone', '请输入正确的手机号码');
                    isValid = false;
                }

                // 邮箱验证
                const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
                if (!emailRegex.test(email)) {
                    showFieldError('email', '请输入正确的邮箱地址');
                    isValid = false;
                }

                // 密码验证
                if (password.length < 6) {
                    showFieldError('password', '密码长度至少6位');
                    isValid = false;
                }

                // 确认密码验证
                if (password !== confirmPassword) {
                    showFieldError('confirmPassword', '两次输入的密码不一致');
                    isValid = false;
                }

                return isValid;
            }

            registerForm.addEventListener('submit', async function(e) {
                e.preventDefault();
                hideMessages();

                if (!validateForm()) {
                    return;
                }

                const formData = {
                    username: document.getElementById('username').value.trim(),
                    phone: document.getElementById('phone').value.trim(),
                    email: document.getElementById('email').value.trim(),
                    password: document.getElementById('password').value,
                    confirmPassword: document.getElementById('confirmPassword').value
                };

                registerBtn.disabled = true;
                registerBtn.textContent = '注册中...';

                try {
                    // 检查用户名是否可用
                    const usernameAvailable = await checkUsernameAvailability(formData.username);
                    if (!usernameAvailable) {
                        showFieldError('username', '用户名已存在');
                        return;
                    }

                    // 检查邮箱是否可用
                    const emailAvailable = await checkEmailAvailability(formData.email);
                    if (!emailAvailable) {
                        showFieldError('email', '邮箱已被注册');
                        return;
                    }

                    const response = await fetch('/api/users/register', {
                        method: 'POST',
                        headers: {
                            'Content-Type': 'application/json'
                        },
                        body: JSON.stringify(formData)
                    });

                    const data = await response.json();

                    if (response.ok && data.success) {
                        showSuccess('注册成功！即将跳转到登录页面...');
                        setTimeout(() => {
                            window.location.href = '/login';
                        }, 2000);
                    } else {
                        showError(data.message || '注册失败，请稍后重试');
                    }
                } catch (error) {
                    console.error('注册错误:', error);
                    showError('网络错误，请稍后重试');
                } finally {
                    registerBtn.disabled = false;
                    registerBtn.textContent = '立即注册';
                }
            });

            // 检查是否已经登录
            const token = localStorage.getItem('token');
            if (token) {
                window.location.href = '/';
            }
        });
    </script>
</body>
</html> 